package com.penton.util.activity;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import com.penton.util.R;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
import android.os.Bundle;
import android.os.Environment;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;

/**
 * 相机案例
 * 
 * @author pengjianbo
 */
public class CameraDemoActivity extends Activity implements
		SurfaceHolder.Callback {

	private SurfaceView mSurfaceView;

	private SurfaceHolder mHolder;

	private Camera mCamera;

	private Button mPaiBtn, mRtPaiBtn, mSaveBtn;

	private AutoFocusCallback mAutoFocusCallback = new AutoFocusCallback();

	private Bitmap mBitmap;

	private String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/DCIM/";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		/* 隐藏标题栏 */
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		/* 设定屏幕显示为横向 */
		this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

		setContentView(R.layout.mycamera);

		mPaiBtn = (Button) findViewById(R.id.pai_btn);
		mPaiBtn.setOnClickListener(new PaiBtnClickListener());

		mRtPaiBtn = (Button) findViewById(R.id.restar_pai_btn);
		mRtPaiBtn.setOnClickListener(new RestartPaiClickListener());

		mSaveBtn = (Button) findViewById(R.id.save_btn);
		mSaveBtn.setOnClickListener(new SaveBtnClickListener());

		mSurfaceView = (SurfaceView) findViewById(R.id.surfaceview);
		mHolder = mSurfaceView.getHolder();
		mHolder.addCallback(this);
		mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		try {
			/* 打开相机 */
			mCamera = Camera.open();
			mCamera.setPreviewDisplay(holder);
		} catch (IOException exception) {
			mCamera.release();
			mCamera = null;
		}
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		initCamera();
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		stopCamera();
		mCamera.release();
		mCamera = null;
	}

	/**
	 * 拍照按钮事件
	 */
	private class PaiBtnClickListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			mCamera.autoFocus(mAutoFocusCallback);
		}
	}

	/**
	 * 重拍按钮事件
	 */
	private class RestartPaiClickListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			mPaiBtn.setVisibility(View.VISIBLE);
			/* 重新设定Camera */
			if (!mBitmap.isRecycled()) {
				mBitmap.recycle();
			}
			stopCamera();
			initCamera();
		}
	}

	/**
	 * 保存图片按钮
	 */
	private class SaveBtnClickListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			
			if (mBitmap != null) {
				if (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
					Toast.makeText(CameraDemoActivity.this, "SD卡不存在", Toast.LENGTH_LONG).show();
				} else {
					try {
						/* 文件不存在就创建 */
						File f = new File(path);

						if (!f.exists()) {
							f.mkdir();
						}
						
						File file = new File(f.getPath() + "save_camera.jpg");
						FileOutputStream bos = new FileOutputStream(file.getPath());
						
						mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
						bos.flush();
						bos.close();
						Toast.makeText(getBaseContext(), "保存到" + file.getPath(), Toast.LENGTH_LONG).show();
						
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
				
				if (!mBitmap.isRecycled()) {
					mBitmap.recycle();
				}
			} else {
				Toast.makeText(getBaseContext(), "没有获取到图片", Toast.LENGTH_LONG).show();
			}

		}
	}

	/**
	 * 初始化相机
	 */
	private void initCamera() {
		if (mCamera != null) {
			try {
				Camera.Parameters parameters = mCamera.getParameters();
				parameters.setPictureFormat(PixelFormat.JPEG);
				parameters.setPictureSize(1024, 768);

				mCamera.setParameters(parameters);
				mCamera.startPreview();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 停止拍照
	 */
	private void stopCamera() {
		if (mCamera != null) {
			try {
				/* 停止预览 */
				mCamera.stopPreview();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public final class AutoFocusCallback implements 
			android.hardware.Camera.AutoFocusCallback {
		public void onAutoFocus(boolean focused, Camera camera) {
			/* 对到焦点拍照 */
			if (focused) {
				if (mCamera != null) {
					mCamera.takePicture(shutterCallback, rawCallback, jpegCallback);
				}
			}
		};

		private ShutterCallback shutterCallback = new ShutterCallback() {
			public void onShutter() {
				/* 按兀快门瞬间会呼?这里的程序 */
			}
		};

		private PictureCallback rawCallback = new PictureCallback() {
			public void onPictureTaken(byte[] data, Camera camera) {
				/* 要处理raw data?写?否 */
			}
		};

		private PictureCallback jpegCallback = new PictureCallback() {
			public void onPictureTaken(byte[] data, Camera camera) {
				/* 取得相片 */
				try {
					/* 取得相片Bitmap对象 */
					mBitmap = BitmapFactory.decodeByteArray(data, 0,
							data.length);

					mPaiBtn.setVisibility(View.GONE);
					mSaveBtn.setVisibility(View.VISIBLE);
					mRtPaiBtn.setVisibility(View.VISIBLE);

					Toast.makeText(getBaseContext(), "拍照成功", Toast.LENGTH_SHORT)
							.show();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		};
	}
	
	
}
